/**
 * 回放界面
 * Created by duwei on 2018/6/13.
 */
var PlayBackWindow = Window.extend({
    _blockLayerOpacity: 0,
    curDataIndex: 0,
    PLAY_INTERVAL: 1.6,
    room: null,
    data: [],
    /**
     * 初始化
     * @param data
     * @param room
     */
    initWindow: function (data, room) {
        this._super();
        var self = this;
        self.room = room;
        cc.log(JSON.stringify(data));
        this._initPlayBackWindow(data);
    },
    /**
     * 点击事件
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.back_searchpai': '_clickBack',
            'root.next_searchpai': '_clickNext',
            'root.pause_searchpai': '_clickPause',
            'root.play_searchpai': '_clickPlay',
            'root.btn_close': '_clickClose'
        };
    },
    /**
     * 上一步
     * @private
     */
    _clickBack: function () {
        var self = this;
        self.unscheduleAllCallbacks();
        for (self.curDataIndex = Math.max(self.curDataIndex - 1, 0); self.curDataIndex >= 0; self.curDataIndex--) {
            if (self.data[self.curDataIndex] && !self.data[self.curDataIndex].code) {
                var uid4001 = 0;
                for (var i = self.curDataIndex + 1; i < self.data.length && self.data[i].code; i++)
                    if (self.data[i].code == 4001 || self.data[i].code == 4003) {
                        uid4001 = self.data[i]['data'].uid;
                        break;
                    }
                if (uid4001)
                    self.room.throwTurnByUid(uid4001);
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                self.room.setAllPai4Replay(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL / 2);
                break;
            }
        }
        if (self.curDataIndex < 0) {
            self.curDataIndex = 0;
            self.room.setReplayProgress(0, 1);
        }
    },
    /**
     * 下一步
     * @private
     */
    _clickNext: function () {
        var self = this;
        self.unscheduleAllCallbacks();
        for (self.curDataIndex = Math.min(self.curDataIndex + 1, (self.data.length > 0) ? self.data.length - 1 : 0); self.curDataIndex < self.data.length; self.curDataIndex++) {
            if (self.data[self.curDataIndex] && !self.data[self.curDataIndex].code) {
                var uid4001 = 0;
                for (var i = self.curDataIndex + 1; i < self.data.length && self.data[i].code; i++)
                    if (self.data[i].code == 4001 || self.data[i].code == 4003) {
                        uid4001 = self.data[i]['data'].uid;
                        break;
                    }
                if (uid4001)
                    self.room.throwTurnByUid(uid4001);
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                self.room.setAllPai4Replay(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL / 2);
                break;
            }
        }

        if (self.curDataIndex >= self.data.length) {
            self.curDataIndex = self.data.length - 1;
            self.room.setReplayProgress(1, 1);
        }
    },
    /**
     * 暂停
     * @private
     */
    _clickPause: function () {
        var self = this;
        cc.director.pause();
        self.status = !self.status;
        self._nodeList['root.pause_searchpai'].setVisible(false);
        self._nodeList['root.play_searchpai'].setVisible(true);
    },
    /**
     * 继续
     * @private
     */
    _clickPlay: function () {
        var self = this;
        cc.director.resume();
        self.status = !self.status;
        self._nodeList['root.play_searchpai'].setVisible(false);
        self._nodeList['root.pause_searchpai'].setVisible(true);
    },
    /**
     * 关闭返回
     * @private
     */
    _clickClose: function () {
        showMessageBox("确定要退出吗?", function () {
            cc.director.resume();
            gameData.isReplay = false;
            showBoard('HallBoard');
            eventManager.dispatchEvent(EventKey.ReceiveZhanJi);
        });
    },
    /**
     * 初始化界面
     * @param data
     * @private
     */
    _initPlayBackWindow: function (data) {
        var self = this;
        self._nodeList['root.play_searchpai'].setVisible(false);
        var jsondata = data || {};
        self.resp4008 = _.isArray(jsondata["4008"]) ? jsondata["4008"][0] : null;

        network.selfRecv(jsondata["3002"] || {});

        var idArrS = jsondata["3005"] || [];
        for (var i = 0; i < idArrS.length; i++) {
            if (idArrS[i]["data"]["uid"] == gameData.uid) {
                network.selfRecv(idArrS[i]);
                break;
            }
        }
        if (i == idArrS.length && idArrS.length != 0) {
            network.selfRecv(idArrS[0]);
        }

        self.data = jsondata["arr"] || [];
        while (true) {
            for (var i = 0; i < self.data.length;) {
                if (self.data[i].code && self.data[i].code == 4004) {
                    var j = i;
                    var arr = [];
                    for (; j < self.data.length; j++) {
                        if (self.data[j].code && self.data[j].code == 4004) {
                            arr.push(self.data[j]);
                        }
                        else
                            break;
                    }
                    if (j - i > 0) {
                        self.data.splice(i, j - i, {
                            code: 40044,
                            data: {arr: arr}
                        });
                        break;
                    }
                }
                else
                    i++;
            }
            if (i == self.data.length)
                break;
        }
        //目前是暂停还是播放状态 true是播放 false暂停
        this.status = true;
        self.room.setAfterGameStart(function () {
            //第一条数据
            self.room.setAllPai4Replay(self.data[self.curDataIndex]);
            self.scheduleId = self.scheduleOnce(function () {
                self.popCommand();
            }, 0.01);
        });
    },
    /**
     * 下一个消息
     */
    popCommand: function () {
        var self = this;
        for (self.curDataIndex++; self.curDataIndex < self.data.length; self.curDataIndex++) {
            if (self.data[self.curDataIndex].code) {
                self.room.setReplayProgress(self.curDataIndex, self.data.length - 1);
                if (self.curDataIndex == self.data.length - 1 && self.resp4008) {
                    self.showJieSuanWindow();
                }
                if (self.data[self.curDataIndex].code == 4003)
                    self.room.throwTurnByUid(self.data[self.curDataIndex]['data'].uid);
                network.selfRecv(self.data[self.curDataIndex]);
                self.scheduleOnce(function () {
                    self.popCommand();
                }, self.PLAY_INTERVAL);
                break;
            }
        }
    },
    /**
     * 显示小结算
     */
    showJieSuanWindow: function () {
        var self = this;
        self.room.showJieSuanWindow(self.resp4008.data);
    }
});